package com.ruoyi.common.utils;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;


/**
* 根据json的key获取key所对应的值
 * by lzx
* */
public class JSONGetDataUtil {

    /**
     * 根据指定的key获取相key对应的值
     * 前提是json中所有的key必须唯一否则会出现后面的覆盖前面的问题
     * 返回json
     * 方法入口
     * */
    public static JSONObject getJsonValue(String json, List<String> needToShowKeyList){
        JSONObject jsonObject = JSON.parseObject(json);
        HashMap<String, Object> hashMap = new HashMap<>();
        jsonLoop(jsonObject,needToShowKeyList,hashMap);
        JSONObject finalJsonObject = new JSONObject();
        toJsonObj(hashMap,finalJsonObject);
        return finalJsonObject;
    }

    public static void jsonLoop(Object object, List<String> needToShowKeyList,HashMap<String, Object> hashMap)  {

        if(object instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) object;
            for (Map.Entry<String, Object> entry: jsonObject.entrySet()) {
                Object o = entry.getValue();

                if(o instanceof String || o instanceof Integer || o instanceof Double || o instanceof Float) {
                    String key = entry.getKey();
                    Object value = entry.getValue();
                    System.out.println("key:" + key + "，value:" + value);
                    //尋找我們需要的字段
                    for(int i=0;i<needToShowKeyList.size();i++){
                        String s = needToShowKeyList.get(i);
                        if(s.equals(key)){
                            hashMap.put(s,value);
                        }
                    }
                } else {
                    jsonLoop(o,needToShowKeyList,hashMap);
                }
            }
        }
        if(object instanceof JSONArray) {
            JSONArray jsonArray = (JSONArray) object;
            for(int i = 0; i < jsonArray.size(); i ++) {
                jsonLoop(jsonArray.get(i),needToShowKeyList,hashMap);
            }
        }
    }

    private static JSONObject toJsonObj(Map<String, Object> map, JSONObject resultJson) {
        Iterator it = map.keySet().iterator();
        while (it.hasNext()) {
            String key = (String) it.next();
            resultJson.put(key, map.get(key));
        }
        return resultJson;
    }


    /**
    * 针对这样的情况 A.B.C.D  其中d代表我们需要获取数据指定的key  那么keySplit对应的值是A,B,C,D
    * json的value不是array的情况
    * rounbinCount 计数器从0开始，记录jsonObject的key的索引  方法调用的时候写0即可
    * 然后将数据结果封装到map里面，例如上面例子A.B.C.D   那么map的key是D
    * */
    public static void getKeyValue(JSONObject obj,String[] keySplit,int rounbinCount,Map<String,Object> map){
        int keyLength = keySplit.length;
        if(keyLength==1){
            try {
                Object o = obj.get(keySplit[0]);
                map.put(keySplit[0],o);
            } catch (Exception e) {
                e.printStackTrace();
                map.put(keySplit[0],null);
            }

        }else if(keyLength>=2 && rounbinCount<=keyLength){
            //JSONObject jsonObject=null;
            if(rounbinCount+1< keyLength){
                obj = obj.getJSONObject(keySplit[rounbinCount]);
                rounbinCount++;
                getKeyValue(obj,keySplit,rounbinCount,map);
            }
            if(rounbinCount+1==keyLength){

                try {
                    Object o = obj.get(keySplit[rounbinCount]);
                    map.put(keySplit[rounbinCount],o);
                } catch (Exception e) {
                    e.printStackTrace();
                    map.put(keySplit[rounbinCount],null);
                }


            }
        }
    }


    /**
     * json的value是array的情况
     * rounbinCount 计数器从0开始，记录jsonObject的key的索引，方法调用的时候写0即可
     * 格式：
     *    B=[C1,C2,C3]   keySplit对应 B=[C1,C2,C3]  其中B是最顶层
     *    A.B=[C1,C2,C3]  keySplit对应 A,B=[C1,C2,C3]   其中A是最顶层
     *    A.B=[C1=[D,E,F]]   keySplit对应 A,B=[C1=[D,E,F]]  其中A是最顶层，B是第二层这样，以此类推
     *    A.B.C=[D*E=[F]]   keySplit对应 A,B,C=[D*E=[F]]  含有的[]包含的内容中有*代表数组中某个jsonObject对象.当前D*E=[F]的意思是取D对象中E数组中的F字段的值
     * 然后将数据结果封装到map里面,并且放在list中返回
     * 其中list中的map的key是以字符串中最深一层出现[]包住的字段
     * */
    public static void getKeyArrayValue(JSONObject obj,String[] keySplit,int rounbinCount,List<Map<String,Object>> list){
        int keyLength = keySplit.length;
        if(keyLength==1){
            String keyOfArray = keySplit[0];
            String[] tempSplit = keyOfArray.split("=");
            String[] keyInArray = tempSplit[1].replace("[", "").replace("]", "").split(",");

            JSONArray jsonArray = obj.getJSONArray(tempSplit[0]);
            for(int k=0;k<jsonArray.size();k++){
                Map<String,Object> map = new HashMap<String,Object>();
                for(int j=0;j<keyInArray.length;j++){
                    //注意这里array里面的数据必须以对象的形式呈现
                    JSONObject arrayEachObj = jsonArray.getJSONObject(k);
                    Object o = arrayEachObj.get(keyInArray[j]);
                    map.put(keyInArray[j],o);
                }
                list.add(map);
            }

        }else if(keyLength>=2 && rounbinCount<=keyLength){
            //A.B=[C=[C1,C2,C3]]
            if(rounbinCount< keyLength){
                String tempStr = keySplit[rounbinCount];
                if(tempStr.contains("=")){
                    //证明是jsonArray
                    int equalIndex = tempStr.indexOf("=");

                    //获取到B
                    String tempKey=tempStr.substring(0,equalIndex);
                    JSONArray jsonArray = obj.getJSONArray(tempKey);

                    //[C1=[D,E,F]]   D,E,F分别代表数组中需要获取的key的值
                    String arrayKeyStr=tempStr.substring(equalIndex+1,tempStr.length());

                    int fisrtMiddleHuoHaoIndex = arrayKeyStr.indexOf("[");
                    int lastMiddleHuoHaoIndex = arrayKeyStr.lastIndexOf("]");

                    arrayKeyStr=arrayKeyStr.substring(fisrtMiddleHuoHaoIndex+1,lastMiddleHuoHaoIndex);

                    if(arrayKeyStr.contains("[")&&arrayKeyStr.contains("]")){

                        if(arrayKeyStr.contains("*")){

                            //将[aggregation_name*buckets=[doc_count]] 去掉前后[] 之后 aggregation_name*buckets=[doc_count]
                            //然后将[aggregation_name*buckets=[doc_count] 中的*换成.
                            arrayKeyStr=arrayKeyStr.replace("*",".");
                            String[] split = arrayKeyStr.split("\\.");
                            rounbinCount=0;
                            for(int k=0;k<jsonArray.size();k++){
                                obj=jsonArray.getJSONObject(k);
                                //证明数组里面还有数组
                                getKeyArrayValue(obj,split,rounbinCount,list);
                            }

                        }else{
                            int equalIndexTemp = arrayKeyStr.indexOf("=");
                            String firstValue=arrayKeyStr.substring(0,equalIndexTemp);
                            String secondValue = arrayKeyStr.substring(equalIndexTemp + 1, arrayKeyStr.length());

                            String[] strings = {arrayKeyStr};

                            rounbinCount=0;
                            for(int k=0;k<jsonArray.size();k++){
                                obj=jsonArray.getJSONObject(k);
                                //证明数组里面还有数组
                                getKeyArrayValue(obj,strings,rounbinCount,list);
                            }
                        }
                    }else{
                        //证明数组里面只有对象 arrayKeyStr为逗号分割的一堆key
                        String[] tempKeySplit = arrayKeyStr.split(",");
                        for(int k=0;k<jsonArray.size();k++){
                            Map<String,Object> map = new HashMap<String,Object>();
                            //注意这里array里面的数据必须以对象的形式呈现
                            JSONObject arrayEachObj = jsonArray.getJSONObject(k);
                            for(int i=0;i<tempKeySplit.length;i++){
                                Object o = arrayEachObj.get(tempKeySplit[i]);
                                if(o!=null){
                                    map.put(tempKeySplit[i],o);
                                }else{
                                    map.put(tempKeySplit[i],"");
                                }
                            }
                            list.add(map);
                        }

                        rounbinCount++;
                    }
                }else{
                    try {
                        if(tempStr.contains("[")&&tempStr.contains("]")){
                            String[] keyInArray = tempStr.replace("[", "").replace("]", "").split(",");
                            Map<String,Object> map = new HashMap<String,Object>();
                            for(String key:keyInArray){
                                Object o = obj.get(key);
                                if(o!=null){
                                    map.put(key,o);
                                }else{
                                    map.put(key,"");
                                }
                            }
                            list.add(map);
                        }else{
                            //证明是jsonObject
                            obj=obj.getJSONObject(tempStr);
                            rounbinCount++;
                            getKeyArrayValue(obj,keySplit,rounbinCount,list);
                        }
                    } catch (Exception e) {
                        //e.printStackTrace();
                        throw new RuntimeException(String.format("JsonCastException: tempStr[%s] json字段为空",tempStr));
                    }
                }
            }
        }


    }



    /**
     * json导出指定字段的excel
     * 其中json字符串保存在文件中 ，可以通过读文件的方式，获取json
     * 参数：
     *    jsonFilePath  放json字符串的文件路径 注意文件内容中json内容是经过压缩成一行的
     *    headerName2JsonFiledMap   表头的名字对应json需要导出的key的值
     *          例如： {姓名：A.B} 这样写 代码逻辑可以参考getKeyValue 和 getKeyArrayValue 方法
     * 注意：谨慎使用
     * * */

    public void exportAllFiledFromJson(String jsonFIlePath,Map<String,String> weNeedTranfromHeaderMap){
        BufferedReader bufferedReader = null;
        JSONObject obj=null;
        try {
            bufferedReader=new BufferedReader(new FileReader(jsonFIlePath));
            String tempString = null;
            while ((tempString = bufferedReader.readLine()) != null) {
                try {
                    obj = JSON.parseObject(tempString);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(bufferedReader!=null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        if(!"{}".equals(obj.toJSONString())){
            //所有数据都用这个dataList为主
            List<Map<String, Object>> dataList=new ArrayList<Map<String,Object>>();
            HashMap<String, Object> map = new HashMap<>();
            List<List<Map<String, Object>>> datadataList=new ArrayList<List<Map<String,Object>>>();

            exportAllFiledFromJson(obj,weNeedTranfromHeaderMap,dataList,map,datadataList);


        }


    }


    public void exportAllFiledFromJson(Object object,Map<String,String> weNeedTranfromHeaderMap,List<Map<String, Object>> dataList, HashMap<String, Object> map,List<List<Map<String, Object>>> datadataList ){
        if(object instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) object;
            for (Map.Entry<String, Object> entry: jsonObject.entrySet()) {
                Object o = entry.getValue();

                if(o instanceof String || o instanceof Integer || o instanceof Double || o instanceof Float) {
                    String key = entry.getKey();
                    Object obj = entry.getValue();

                    map.put(key,obj);
                    dataList.add(map);
                    datadataList.add(dataList);


                } else {
                    //jsonLoop(o,needToShowKeyList,hashMap);
                    exportAllFiledFromJson(o,weNeedTranfromHeaderMap,dataList,map,datadataList);
                }
            }
        }
        if(object instanceof JSONArray) {
            JSONArray jsonArray = (JSONArray) object;
            for(int i = 0; i < jsonArray.size(); i ++) {
                //重新构造map
                HashMap<String, Object> rebuiltMap = new HashMap<>();

                for(String key:map.keySet()){
                    Object key1 = map.get("key");
                    rebuiltMap.put(key,key1);
                }

                //jsonLoop(jsonArray.get(i),needToShowKeyList,hashMap);
                exportAllFiledFromJson(jsonArray.get(i),weNeedTranfromHeaderMap,dataList,rebuiltMap,datadataList);
            }
        }
    }




    /**
     *备份
     * **/

     public void exportWeNeedFiledDataExcel(String jsonFilePath,Map<String,String> headerName2JsonFiledMap){

         //所有数据都用这个dataList为主
         List<Map<String, Object>> dataList=new ArrayList<Map<String,Object>>();
         //表頭名字的集合
         Set<String> headerSet=new HashSet<String>();

         String[] header  =null;

         BufferedReader bufferedReader = null;

         try {
             bufferedReader=new BufferedReader(new FileReader(jsonFilePath));
             String tempString = null;
             while ((tempString = bufferedReader.readLine()) != null) {
                 try {
                     JSONObject obj = JSON.parseObject(tempString);
                     Map<String,Object> finalMap=new HashMap<String,Object>();
                     List<Map<String,Object>> tempList=null;

                     for (String headerName:headerName2JsonFiledMap.keySet()){


                         //excel的列表头
                         headerSet.add(headerName);

                         String weNeedJsonKey=headerName2JsonFiledMap.get(headerName);
                         String[] split = weNeedJsonKey.split("\\.");

                         //这里需要判断一下weNeedJsonKey里面是否含有=号，然后根据约定好的规则,然后进行不同的处理
                         if(weNeedJsonKey.contains("=")){
                             tempList=new ArrayList<Map<String,Object>>();
                             getKeyArrayValue(obj,split,0,tempList);
                             //换key的名字，换成自己的定义的
                             int size = tempList.size();
                             for(int k=0;k<size;k++){
                                 Map<String, Object> map = tempList.get(k);
                                 HashMap<String, Object> transferMap = new HashMap<>();
                                 for(String key:map.keySet()){
                                     transferMap.put(headerName,map.get(key));
                                 }
                                 tempList.add(transferMap);
                                 tempList.remove(map);
                             }
                             if(dataList.size()>0){
                                //证明已经有值了，故追加即可
                                for(int kk=0;kk<dataList.size();kk++){
                                    Map<String, Object> map = dataList.get(kk);
                                    for(int gg=0;gg<tempList.size();gg++){
                                        Map<String, Object> map1 = tempList.get(gg);
                                        for(String key:map1.keySet()){
                                            map.put(key,map1.get(key));
                                        }
                                    }

                                }
                             }else{
                                 dataList=tempList;
                             }
                         }else{
                             Map<String,Object> dataMap=new HashMap<String,Object>();
                             getKeyValue(obj,split,0,dataMap);
                             //dataList.add(dataMap);

                             if(tempList!=null && tempList.size()>0){
                                 for (int i=0;i<tempList.size();i++){
                                     Map<String, Object> map = tempList.get(i);
                                     for(String key:dataMap.keySet()){
                                         map.put(key,dataMap.get(key));
                                     }

                                 }
                                 dataList=tempList;
                             }else{
                                 //转化表头的Map
                                 HashMap<String, Object> transferMap = new HashMap<>();
                                 for(String key:dataMap.keySet()){
                                     transferMap.put(headerName,dataMap.get(key));
                                 }
                                 dataList.add(transferMap);
                             }
                         }

                     }

                 } catch (Exception e) {
                    e.printStackTrace();
                 }
             }
         } catch (IOException e) {
             e.printStackTrace();
         } finally {
             if(bufferedReader!=null){
                 try {
                     bufferedReader.close();
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
             }
         }

         header = headerSet.toArray(new String[headerSet.size()]);
         //header  = "记录id,时间,品牌,品类,爬虫关键词,平台,用户,URL,内容,指标".split(",");
        // FileHelper.export2Excel(header, dataList, "D:\\xxx\\JSOnExport.xlsx");
     }



    public static void main(String[] args) {
        String json = "{\"TITLE\":\"Json Title\",\"FORM\":{\"USERNAME\":\"Rick and Morty\"},\"ARRAY\":[{\"FIRST\":\"Rick\"},{\"LAST\":\"Morty\"}]}";
        JSONObject jsonObject = JSON.parseObject(json);

        //获取所有的json的值，会有后面覆盖前面的key的问题
        List<String> needToShowKeyList = Arrays.asList("TITLE,LAST".split(","));
        JSONObject value = getJsonValue(json, needToShowKeyList);
        System.out.println(value.toJSONString());



        json="{\"api\":\"mtop.mediaplatform.video.livedetail.itemlist.withpaginationv5\",\"data\":{\"subVideoItemList\":{},\"totalNum\":27,\"starList\":[],\"brandCard\":{\"tmall\":1321,\"ff\":true},\"exclusiveIcons\":[\"//gw.alicdn.com/mt/TB1cUh7b4SYBuNjSsphXXbGvVXa-153-39.png\",\"//gw.alicdn.com/mt/TB1FKdNeAyWBuNjy0FpXXassXXa-153-39.png\"]},\"ret\":[\"SUCCESS::调用成功\"],\"v\":\"1.0\"}";
        JSONObject jsonObject2 = JSON.parseObject(json);

        String[] keySplit="data.brandCard.ff".split("\\.");
        Map<String,Object> map = new HashMap<>();
        getKeyValue(jsonObject2, keySplit, 0,map);
        System.out.println("getJSonVlaue的值是："+map.toString());


        json="{\"A\":{\"B\":[{\"C1\":111},{\"C2\":222},{\"C3\":333}]},\"D\":[{\"E\":11231,\"F\":11231,\"G\":11231}],\"E\":{\"F\":[{\"G\":[{\"H1\":1234},{\"H2\":1234},{\"H3\":1234}]}]}}";
        JSONObject jsonObject3 = JSON.parseObject(json);
        String[] keySplit2="A.B=[C1,C2,C3]".split("\\.");
        List<Map<String, Object>> list = new ArrayList<>();
        getKeyArrayValue(jsonObject3,keySplit2,0,list);
        System.out.println("某个对象中的单一的Array-->"+list.toString());


        json="{\"A\":{\"B\":[{\"C1\":111},{\"C2\":222},{\"C3\":333}]},\"D\":[{\"E\":11231,\"F\":11231,\"G\":11231}],\"E\":{\"F\":[{\"G\":[{\"H1\":1234},{\"H2\":1234},{\"H3\":1234}]}]},\"FF\":true}";
        JSONObject jsonObject4 = JSON.parseObject(json);

        String[] keySplit3="D=[E,F,G]".split("\\.");
        List<Map<String, Object>> list2 = new ArrayList<>();
        getKeyArrayValue(jsonObject4,keySplit3,0,list2);
        System.out.println("单一的Array-->"+list2.toString());


        String[] keySplit4="E.F=[G=[H1,H2,H3]]".split("\\.");
        List<Map<String, Object>> list3 = new ArrayList<>();
        getKeyArrayValue(jsonObject4,keySplit4,0,list3);
        System.out.println("复合的Array-->"+list3.toString());


        json="{\"took\":27,\"timed_out\":false,\"_shards\":{\"total\":12,\"successful\":12,\"failed\":0},\"hits\":{\"total\":152050,\"max_score\":0,\"hits\":[]},\"aggregations\":{\"aggregation_name\":{\"doc_count_error_upper_bound\":0,\"sum_other_doc_count\":1300,\"buckets\":[{\"key\":\"新浪新闻\",\"doc_count\":78767,\"aggregation_name\":{\"buckets\":[{\"key_as_string\":\"2020-04-13T00:00:00.000+08:00\",\"key\":1586707200000,\"doc_count\":2264},{\"key_as_string\":\"2020-04-12T00:00:00.000+08:00\",\"key\":1586620800000,\"doc_count\":4486},{\"key_as_string\":\"2020-04-11T00:00:00.000+08:00\",\"key\":1586534400000,\"doc_count\":4390},{\"key_as_string\":\"2020-04-10T00:00:00.000+08:00\",\"key\":1586448000000,\"doc_count\":5854},{\"key_as_string\":\"2020-04-09T00:00:00.000+08:00\",\"key\":1586361600000,\"doc_count\":5546},{\"key_as_string\":\"2020-04-08T00:00:00.000+08:00\",\"key\":1586275200000,\"doc_count\":6325},{\"key_as_string\":\"2020-04-07T00:00:00.000+08:00\",\"key\":1586188800000,\"doc_count\":6954},{\"key_as_string\":\"2020-04-06T00:00:00.000+08:00\",\"key\":1586102400000,\"doc_count\":6229},{\"key_as_string\":\"2020-04-05T00:00:00.000+08:00\",\"key\":1586016000000,\"doc_count\":6413},{\"key_as_string\":\"2020-04-04T00:00:00.000+08:00\",\"key\":1585929600000,\"doc_count\":5713},{\"key_as_string\":\"2020-04-03T00:00:00.000+08:00\",\"key\":1585843200000,\"doc_count\":8147},{\"key_as_string\":\"2020-04-02T00:00:00.000+08:00\",\"key\":1585756800000,\"doc_count\":8146},{\"key_as_string\":\"2020-04-01T00:00:00.000+08:00\",\"key\":1585670400000,\"doc_count\":8300}]}},{\"key\":\"腾讯新闻\",\"doc_count\":35828,\"aggregation_name\":{\"buckets\":[{\"key_as_string\":\"2020-04-13T00:00:00.000+08:00\",\"key\":1586707200000,\"doc_count\":1140},{\"key_as_string\":\"2020-04-12T00:00:00.000+08:00\",\"key\":1586620800000,\"doc_count\":1846},{\"key_as_string\":\"2020-04-11T00:00:00.000+08:00\",\"key\":1586534400000,\"doc_count\":2048},{\"key_as_string\":\"2020-04-10T00:00:00.000+08:00\",\"key\":1586448000000,\"doc_count\":3232},{\"key_as_string\":\"2020-04-09T00:00:00.000+08:00\",\"key\":1586361600000,\"doc_count\":3764},{\"key_as_string\":\"2020-04-08T00:00:00.000+08:00\",\"key\":1586275200000,\"doc_count\":3848},{\"key_as_string\":\"2020-04-07T00:00:00.000+08:00\",\"key\":1586188800000,\"doc_count\":3702},{\"key_as_string\":\"2020-04-06T00:00:00.000+08:00\",\"key\":1586102400000,\"doc_count\":2382},{\"key_as_string\":\"2020-04-05T00:00:00.000+08:00\",\"key\":1586016000000,\"doc_count\":2046},{\"key_as_string\":\"2020-04-04T00:00:00.000+08:00\",\"key\":1585929600000,\"doc_count\":1640},{\"key_as_string\":\"2020-04-03T00:00:00.000+08:00\",\"key\":1585843200000,\"doc_count\":3118},{\"key_as_string\":\"2020-04-02T00:00:00.000+08:00\",\"key\":1585756800000,\"doc_count\":3422},{\"key_as_string\":\"2020-04-01T00:00:00.000+08:00\",\"key\":1585670400000,\"doc_count\":3640}]}},{\"key\":\"百度资讯\",\"doc_count\":22777,\"aggregation_name\":{\"buckets\":[{\"key_as_string\":\"2020-04-13T00:00:00.000+08:00\",\"key\":1586707200000,\"doc_count\":835},{\"key_as_string\":\"2020-04-12T00:00:00.000+08:00\",\"key\":1586620800000,\"doc_count\":1249},{\"key_as_string\":\"2020-04-11T00:00:00.000+08:00\",\"key\":1586534400000,\"doc_count\":1406},{\"key_as_string\":\"2020-04-10T00:00:00.000+08:00\",\"key\":1586448000000,\"doc_count\":1980},{\"key_as_string\":\"2020-04-09T00:00:00.000+08:00\",\"key\":1586361600000,\"doc_count\":2170},{\"key_as_string\":\"2020-04-08T00:00:00.000+08:00\",\"key\":1586275200000,\"doc_count\":2434},{\"key_as_string\":\"2020-04-07T00:00:00.000+08:00\",\"key\":1586188800000,\"doc_count\":2236},{\"key_as_string\":\"2020-04-06T00:00:00.000+08:00\",\"key\":1586102400000,\"doc_count\":1519},{\"key_as_string\":\"2020-04-05T00:00:00.000+08:00\",\"key\":1586016000000,\"doc_count\":1270},{\"key_as_string\":\"2020-04-04T00:00:00.000+08:00\",\"key\":1585929600000,\"doc_count\":1051},{\"key_as_string\":\"2020-04-03T00:00:00.000+08:00\",\"key\":1585843200000,\"doc_count\":2091},{\"key_as_string\":\"2020-04-02T00:00:00.000+08:00\",\"key\":1585756800000,\"doc_count\":2172},{\"key_as_string\":\"2020-04-01T00:00:00.000+08:00\",\"key\":1585670400000,\"doc_count\":2364}]}},{\"key\":\"一点资讯\",\"doc_count\":7597,\"aggregation_name\":{\"buckets\":[{\"key_as_string\":\"2020-04-13T00:00:00.000+08:00\",\"key\":1586707200000,\"doc_count\":254},{\"key_as_string\":\"2020-04-12T00:00:00.000+08:00\",\"key\":1586620800000,\"doc_count\":395},{\"key_as_string\":\"2020-04-11T00:00:00.000+08:00\",\"key\":1586534400000,\"doc_count\":472},{\"key_as_string\":\"2020-04-10T00:00:00.000+08:00\",\"key\":1586448000000,\"doc_count\":691},{\"key_as_string\":\"2020-04-09T00:00:00.000+08:00\",\"key\":1586361600000,\"doc_count\":666},{\"key_as_string\":\"2020-04-08T00:00:00.000+08:00\",\"key\":1586275200000,\"doc_count\":789},{\"key_as_string\":\"2020-04-07T00:00:00.000+08:00\",\"key\":1586188800000,\"doc_count\":736},{\"key_as_string\":\"2020-04-06T00:00:00.000+08:00\",\"key\":1586102400000,\"doc_count\":495},{\"key_as_string\":\"2020-04-05T00:00:00.000+08:00\",\"key\":1586016000000,\"doc_count\":465},{\"key_as_string\":\"2020-04-04T00:00:00.000+08:00\",\"key\":1585929600000,\"doc_count\":393},{\"key_as_string\":\"2020-04-03T00:00:00.000+08:00\",\"key\":1585843200000,\"doc_count\":708},{\"key_as_string\":\"2020-04-02T00:00:00.000+08:00\",\"key\":1585756800000,\"doc_count\":744},{\"key_as_string\":\"2020-04-01T00:00:00.000+08:00\",\"key\":1585670400000,\"doc_count\":789}]}},{\"key\":\"百度网页\",\"doc_count\":1813,\"aggregation_name\":{\"buckets\":[{\"key_as_string\":\"2020-04-13T00:00:00.000+08:00\",\"key\":1586707200000,\"doc_count\":24},{\"key_as_string\":\"2020-04-12T00:00:00.000+08:00\",\"key\":1586620800000,\"doc_count\":52},{\"key_as_string\":\"2020-04-11T00:00:00.000+08:00\",\"key\":1586534400000,\"doc_count\":87},{\"key_as_string\":\"2020-04-10T00:00:00.000+08:00\",\"key\":1586448000000,\"doc_count\":136},{\"key_as_string\":\"2020-04-09T00:00:00.000+08:00\",\"key\":1586361600000,\"doc_count\":164},{\"key_as_string\":\"2020-04-08T00:00:00.000+08:00\",\"key\":1586275200000,\"doc_count\":189},{\"key_as_string\":\"2020-04-07T00:00:00.000+08:00\",\"key\":1586188800000,\"doc_count\":216},{\"key_as_string\":\"2020-04-06T00:00:00.000+08:00\",\"key\":1586102400000,\"doc_count\":137},{\"key_as_string\":\"2020-04-05T00:00:00.000+08:00\",\"key\":1586016000000,\"doc_count\":133},{\"key_as_string\":\"2020-04-04T00:00:00.000+08:00\",\"key\":1585929600000,\"doc_count\":132},{\"key_as_string\":\"2020-04-03T00:00:00.000+08:00\",\"key\":1585843200000,\"doc_count\":174},{\"key_as_string\":\"2020-04-02T00:00:00.000+08:00\",\"key\":1585756800000,\"doc_count\":176},{\"key_as_string\":\"2020-04-01T00:00:00.000+08:00\",\"key\":1585670400000,\"doc_count\":193}]}},{\"key\":\"今日头条\",\"doc_count\":1641,\"aggregation_name\":{\"buckets\":[{\"key_as_string\":\"2020-04-13T00:00:00.000+08:00\",\"key\":1586707200000,\"doc_count\":56},{\"key_as_string\":\"2020-04-12T00:00:00.000+08:00\",\"key\":1586620800000,\"doc_count\":72},{\"key_as_string\":\"2020-04-11T00:00:00.000+08:00\",\"key\":1586534400000,\"doc_count\":101},{\"key_as_string\":\"2020-04-10T00:00:00.000+08:00\",\"key\":1586448000000,\"doc_count\":150},{\"key_as_string\":\"2020-04-09T00:00:00.000+08:00\",\"key\":1586361600000,\"doc_count\":141},{\"key_as_string\":\"2020-04-08T00:00:00.000+08:00\",\"key\":1586275200000,\"doc_count\":156},{\"key_as_string\":\"2020-04-07T00:00:00.000+08:00\",\"key\":1586188800000,\"doc_count\":168},{\"key_as_string\":\"2020-04-06T00:00:00.000+08:00\",\"key\":1586102400000,\"doc_count\":130},{\"key_as_string\":\"2020-04-05T00:00:00.000+08:00\",\"key\":1586016000000,\"doc_count\":92},{\"key_as_string\":\"2020-04-04T00:00:00.000+08:00\",\"key\":1585929600000,\"doc_count\":82},{\"key_as_string\":\"2020-04-03T00:00:00.000+08:00\",\"key\":1585843200000,\"doc_count\":159},{\"key_as_string\":\"2020-04-02T00:00:00.000+08:00\",\"key\":1585756800000,\"doc_count\":154},{\"key_as_string\":\"2020-04-01T00:00:00.000+08:00\",\"key\":1585670400000,\"doc_count\":180}]}},{\"key\":\"中关村在线\",\"doc_count\":688,\"aggregation_name\":{\"buckets\":[{\"key_as_string\":\"2020-04-13T00:00:00.000+08:00\",\"key\":1586707200000,\"doc_count\":20},{\"key_as_string\":\"2020-04-12T00:00:00.000+08:00\",\"key\":1586620800000,\"doc_count\":10},{\"key_as_string\":\"2020-04-11T00:00:00.000+08:00\",\"key\":1586534400000,\"doc_count\":20},{\"key_as_string\":\"2020-04-10T00:00:00.000+08:00\",\"key\":1586448000000,\"doc_count\":69},{\"key_as_string\":\"2020-04-09T00:00:00.000+08:00\",\"key\":1586361600000,\"doc_count\":102},{\"key_as_string\":\"2020-04-08T00:00:00.000+08:00\",\"key\":1586275200000,\"doc_count\":123},{\"key_as_string\":\"2020-04-07T00:00:00.000+08:00\",\"key\":1586188800000,\"doc_count\":107},{\"key_as_string\":\"2020-04-06T00:00:00.000+08:00\",\"key\":1586102400000,\"doc_count\":10},{\"key_as_string\":\"2020-04-05T00:00:00.000+08:00\",\"key\":1586016000000,\"doc_count\":11},{\"key_as_string\":\"2020-04-04T00:00:00.000+08:00\",\"key\":1585929600000,\"doc_count\":28},{\"key_as_string\":\"2020-04-03T00:00:00.000+08:00\",\"key\":1585843200000,\"doc_count\":66},{\"key_as_string\":\"2020-04-02T00:00:00.000+08:00\",\"key\":1585756800000,\"doc_count\":58},{\"key_as_string\":\"2020-04-01T00:00:00.000+08:00\",\"key\":1585670400000,\"doc_count\":64}]}},{\"key\":\"搜狐\",\"doc_count\":687,\"aggregation_name\":{\"buckets\":[{\"key_as_string\":\"2020-04-13T00:00:00.000+08:00\",\"key\":1586707200000,\"doc_count\":30},{\"key_as_string\":\"2020-04-12T00:00:00.000+08:00\",\"key\":1586620800000,\"doc_count\":41},{\"key_as_string\":\"2020-04-11T00:00:00.000+08:00\",\"key\":1586534400000,\"doc_count\":33},{\"key_as_string\":\"2020-04-10T00:00:00.000+08:00\",\"key\":1586448000000,\"doc_count\":57},{\"key_as_string\":\"2020-04-09T00:00:00.000+08:00\",\"key\":1586361600000,\"doc_count\":79},{\"key_as_string\":\"2020-04-08T00:00:00.000+08:00\",\"key\":1586275200000,\"doc_count\":70},{\"key_as_string\":\"2020-04-07T00:00:00.000+08:00\",\"key\":1586188800000,\"doc_count\":64},{\"key_as_string\":\"2020-04-06T00:00:00.000+08:00\",\"key\":1586102400000,\"doc_count\":61},{\"key_as_string\":\"2020-04-05T00:00:00.000+08:00\",\"key\":1586016000000,\"doc_count\":31},{\"key_as_string\":\"2020-04-04T00:00:00.000+08:00\",\"key\":1585929600000,\"doc_count\":28},{\"key_as_string\":\"2020-04-03T00:00:00.000+08:00\",\"key\":1585843200000,\"doc_count\":63},{\"key_as_string\":\"2020-04-02T00:00:00.000+08:00\",\"key\":1585756800000,\"doc_count\":50},{\"key_as_string\":\"2020-04-01T00:00:00.000+08:00\",\"key\":1585670400000,\"doc_count\":80}]}},{\"key\":\"快科技\",\"doc_count\":584,\"aggregation_name\":{\"buckets\":[{\"key_as_string\":\"2020-04-12T00:00:00.000+08:00\",\"key\":1586620800000,\"doc_count\":10},{\"key_as_string\":\"2020-04-11T00:00:00.000+08:00\",\"key\":1586534400000,\"doc_count\":26},{\"key_as_string\":\"2020-04-10T00:00:00.000+08:00\",\"key\":1586448000000,\"doc_count\":52},{\"key_as_string\":\"2020-04-09T00:00:00.000+08:00\",\"key\":1586361600000,\"doc_count\":92},{\"key_as_string\":\"2020-04-08T00:00:00.000+08:00\",\"key\":1586275200000,\"doc_count\":94},{\"key_as_string\":\"2020-04-07T00:00:00.000+08:00\",\"key\":1586188800000,\"doc_count\":72},{\"key_as_string\":\"2020-04-06T00:00:00.000+08:00\",\"key\":1586102400000,\"doc_count\":26},{\"key_as_string\":\"2020-04-05T00:00:00.000+08:00\",\"key\":1586016000000,\"doc_count\":24},{\"key_as_string\":\"2020-04-04T00:00:00.000+08:00\",\"key\":1585929600000,\"doc_count\":20},{\"key_as_string\":\"2020-04-03T00:00:00.000+08:00\",\"key\":1585843200000,\"doc_count\":51},{\"key_as_string\":\"2020-04-02T00:00:00.000+08:00\",\"key\":1585756800000,\"doc_count\":59},{\"key_as_string\":\"2020-04-01T00:00:00.000+08:00\",\"key\":1585670400000,\"doc_count\":58}]}},{\"key\":\"手机中国\",\"doc_count\":368,\"aggregation_name\":{\"buckets\":[{\"key_as_string\":\"2020-04-13T00:00:00.000+08:00\",\"key\":1586707200000,\"doc_count\":18},{\"key_as_string\":\"2020-04-12T00:00:00.000+08:00\",\"key\":1586620800000,\"doc_count\":8},{\"key_as_string\":\"2020-04-11T00:00:00.000+08:00\",\"key\":1586534400000,\"doc_count\":8},{\"key_as_string\":\"2020-04-10T00:00:00.000+08:00\",\"key\":1586448000000,\"doc_count\":39},{\"key_as_string\":\"2020-04-09T00:00:00.000+08:00\",\"key\":1586361600000,\"doc_count\":41},{\"key_as_string\":\"2020-04-08T00:00:00.000+08:00\",\"key\":1586275200000,\"doc_count\":57},{\"key_as_string\":\"2020-04-07T00:00:00.000+08:00\",\"key\":1586188800000,\"doc_count\":49},{\"key_as_string\":\"2020-04-06T00:00:00.000+08:00\",\"key\":1586102400000,\"doc_count\":7},{\"key_as_string\":\"2020-04-05T00:00:00.000+08:00\",\"key\":1586016000000,\"doc_count\":5},{\"key_as_string\":\"2020-04-04T00:00:00.000+08:00\",\"key\":1585929600000,\"doc_count\":8},{\"key_as_string\":\"2020-04-03T00:00:00.000+08:00\",\"key\":1585843200000,\"doc_count\":41},{\"key_as_string\":\"2020-04-02T00:00:00.000+08:00\",\"key\":1585756800000,\"doc_count\":43},{\"key_as_string\":\"2020-04-01T00:00:00.000+08:00\",\"key\":1585670400000,\"doc_count\":44}]}}]}}}";
        JSONObject jsonObject5 = JSON.parseObject(json);

        String[] keySplit5="aggregations.aggregation_name.buckets=[key,doc_count]".split("\\.");
        List<Map<String, Object>> list5 = new ArrayList<>();
        getKeyArrayValue(jsonObject5,keySplit5,0,list5);
        System.out.println("xxx1的Array-->"+list5.toString());


        String[] keySplit6="aggregations.aggregation_name.buckets=[doc_count]".split("\\.");
        List<Map<String, Object>> list6 = new ArrayList<>();
        getKeyArrayValue(jsonObject5,keySplit6,0,list6);
        System.out.println("xxx2的Array-->"+list6.toString());



        String[] keySplit7="aggregations.aggregation_name.buckets=[aggregation_name*buckets=[doc_count]]".split("\\.");
        List<Map<String, Object>> list7 = new ArrayList<>();
        getKeyArrayValue(jsonObject5,keySplit7,0,list7);
        System.out.println("xxx3的Array-->"+list7.toString());


        json="{\"took\":3073,\"timed_out\":false,\"_shards\":{\"total\":12,\"successful\":12,\"failed\":0},\"hits\":{\"total\":16579,\"max_score\":11.095416,\"hits\":[{\"_index\":\"midea-public-realty_opinion_nlp-v1\",\"_type\":\"headlines\",\"_id\":\"2857651798bc9933219d122635cfa04d\",\"_score\":11.095416,\"_source\":{\"id\":\"2857651798bc9933219d122635cfa04d\"}},{\"_index\":\"midea-public-realty_opinion_nlp-v1\",\"_type\":\"headlines\",\"_id\":\"2857651798bc9933219d122635cfa04d\",\"_score\":11.095416,\"_source\":{\"id\":\"3957651798bc9933219d122647cfa04d\"}}]}}";
        JSONObject j9 = JSON.parseObject(json);
        String[] keySplit8="hits.hits=[_source*[id,id2]]".split("\\.");
        List<Map<String, Object>> list8 = new ArrayList<>();
        getKeyArrayValue(j9,keySplit8,0,list8);
        System.out.println("hits.hits=[_source*[id,id2]] -->"+list8.toString());

    }
}
